<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.id"
        placeholder="商品ID"
        style="width: 140px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.good_name"
        placeholder="商品名"
        style="width: 140px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.store_id"
        placeholder="店铺ID"
        style="width: 140px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.store_name"
        placeholder="店铺名"
        style="width: 140px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.price"
        type="number"
        min="0"
        step="1"
        placeholder="价格"
        style="width: 140px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.user_name"
        placeholder="账号名"
        style="width: 140px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      max-height="500px"
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="id" align="center" min-width="12%">
        <template slot-scope="{row}">
          <a
            :href="'https://mobile.yangkeduo.com/goods.html?goods_id='+row.id"
            target="_blank"
            style="color: blue;"
          >
            {{ row.id }}
          </a>
        </template>
      </el-table-column>
      <el-table-column label="名称" min-width="20%" align="center">
        <template slot-scope="{row}">
          <span>{{ row.goods_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" min-width="8%" align="center">
        <template slot-scope="{row}">
          <el-image style="width: 75px; height: 75px" :src="row.thumb_url" />
        </template>
      </el-table-column>
      <el-table-column label="价格" min-width="8%" align="center">
        <template slot-scope="{row}">
          <span>{{ row.price / 100 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="轮询次数" min-width="8%" align="center">
        <template slot-scope="{row}">
          <span>{{ row.loop_times }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总成交" min-width="7%" align="center">
        <template slot-scope="{row}">
          <span>{{ row.done_times }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当日成交" min-width="7%" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.today_num }}</span>
        </template>
      </el-table-column>
      <el-table-column label="店铺ID" min-width="10%" align="center">
        <template slot-scope="{row}">
          <span>{{ row.store_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="店铺名" min-width="10%" align="center">
        <template slot-scope="{row}">
          <span>{{ row.store_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属客户" min-width="10%" align="center">
        <template slot-scope="{row}">
          <span>{{ row.user_name }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="轮询" min-width="8%" align="center">
        <template slot-scope="{row}">
          <el-switch
            v-model="row.loop"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="toggleLoop(row)"
          />
        </template>
      </el-table-column> -->
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="fetchList"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getList, setLoop } from '@/api/goods'
import { Message } from 'element-ui'
export default {
  name: 'Goods',
  components: { Pagination },
  data() {
    return {
      listLoading: false,
      total: 0,
      list: [],
      listQuery: {
        id: '',
        store_id: '',
        good_name: '',
        price: undefined,
        user_name: '',
        page: 1,
        limit: 10
      }
    }
  },
  created() {
    //  从商店页面跳转过来的
    this.listQuery = { ...this.listQuery, store_id: this.$route.query.store_id }
    this.fetchList()
  },
  methods: {
    fetchList() {
      this.listLoading = true
      const query = { ...this.listQuery, price: this.listQuery.price * 100 }
      getList(query).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.fetchList()
    },
    toggleLoop(row) {
      setLoop(row.id, row.loop).then(response => Message.success('设置成功'))
    }
  }
}
</script>

<style scoped>

</style>
